//
//  AppDelegate.swift
//  testZD
//
//  Created by 湛亚磊 on 2020/9/18.
//  Copyright © 2020 湛亚磊. All rights reserved.
//

import UIKit
import SnapKit
///倍速播放的控制view
open class LivePlayerControllerView: ZFPlayerControlView {
    
    public var currentSpeed : Int = 0

    public var speedArr:[String] = ["1.0x","1.5x","2.0x"]
    ///倍速的回调
    private var speedBlockL:((Int)->())?
    
    private lazy var speedTableView:UITableView = {
        let bgview = UITableView(frame: CGRect(x: UIScreen.main.bounds.size.width-200, y: 0, width: 200, height: UIScreen.main.bounds.size.width), style: .plain)
        bgview.separatorStyle = .none
        bgview.alpha = 0.7
        return bgview
    }()

    public lazy var speedbutton:UIButton = {
       let speedbutton = UIButton()
        speedbutton.setTitle("倍速", for: .normal)
        speedbutton.titleLabel?.font=UIFont.systemFont(ofSize: 13)
        speedbutton.setTitleColor(.white, for: .normal)
        speedbutton.backgroundColor = .clear
        speedbutton.titleLabel?.textAlignment = .center
        speedbutton.layer.cornerRadius=3
        speedbutton.layer.borderWidth=1
        speedbutton.layer.borderColor = UIColor.white.cgColor
        speedbutton.addTarget(self, action: #selector(speedClick), for: .touchUpInside)
        return speedbutton
    }()
    
    

    ///控制变量
    private var isChange:Bool=false
    
    ///代码只执行一次
    private lazy var oneCode:Void={
        landScapeControlView.slider.frame.size.width-=30
        landScapeControlView.totalTimeLabel.frame.origin.x-=30
    }()
    
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        controlViewAppearedCallback={[weak self](isShow) in
            let currentOrientation = UIDevice.current.orientation
            if currentOrientation.isLandscape && isShow {
                self?.changeFrame()
                self!.isChange = !self!.isChange
            }
        }
    }
    
    private func changeFrame()  {
        if isChange{
            _ = oneCode
        }
    }

    
    public override func layoutSubviews() {
        super.layoutSubviews()
        setSubViewsL()
    }

    ///倍速 横屏block
    public func postBlockL(withBlock block:((Int)->())?)  {
        speedBlockL = block
    }

    public override func gestureTriggerCondition(_ gestureControl: ZFPlayerGestureControl, gestureType: ZFPlayerGestureType, gestureRecognizer: UIGestureRecognizer, touch: UITouch) -> Bool {
        if NSStringFromClass((touch.view?.classForCoder)!) == "UITableViewCellContentView"{
            return false
        }else{
            return true
        }
    }

    ///  单击
    public override func gestureSingleTapped(_ gestureControl: ZFPlayerGestureControl) {
        super.gestureSingleTapped(gestureControl)
        speedTableView.removeFromSuperview()
    }

    
    /// 设置倍速播放
    private func setSubViewsL() {
        self.landScapeControlView.bottomToolView.addSubview(speedbutton)
        speedbutton.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.landScapeControlView.playOrPauseBtn.snp.centerY)
            make.right.equalTo(landScapeControlView.bottomToolView.snp.right).offset(-15)
            make.width.equalTo(50)
            make.height.equalTo(30)
        }
    }
    
    @objc public func speedClick(){
        speedTableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
        speedTableView.delegate = self
        speedTableView.dataSource = self
        self.addSubview(speedTableView)
        speedTableView.snp.makeConstraints { (make) in
            make.right.equalTo(self.snp.right)
            make.top.equalTo(self.snp.top)
            make.width.equalTo(200)
            make.height.equalTo(UIScreen.main.bounds.size.height)
        }
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LivePlayerControllerView:UITableViewDelegate,UITableViewDataSource{
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return speedArr.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.backgroundColor = UIColor.black
        cell.selectionStyle = .none
        cell.contentView.alpha = 0.7
        cell.textLabel?.textAlignment = .center
        cell.textLabel?.textColor = (indexPath.row == self.currentSpeed ? UIColor.green :UIColor.white)
        cell.textLabel?.text = speedArr[indexPath.row]
       return cell
    }

    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return UIScreen.main.bounds.size.height/CGFloat(speedArr.count)
    }

    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.currentSpeed = indexPath.row
        speedBlockL?(indexPath.row)
        speedTableView.reloadData()
        speedTableView.removeFromSuperview()
    }
}
